package io.github.talelin.latticy.controller.v1;

import io.github.talelin.latticy.service.UserAuditService;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import io.github.talelin.latticy.vo.UserAuditVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * 用户审核控制器
 * @作者 xiaohengheng
 * @微信公号 林间有风
 * @开源项目 $ http://7yue.pro
 * @免费专栏 $ http://course.7yue.pro
 * @我的课程 $ http://imooc.com/t/4294850
 * @创建时间 2024-01-15
 */
@RestController
@RequestMapping("/v1/user-audit")
@Validated
public class UserAuditController {

    @Autowired
    private UserAuditService userAuditService;

    /**
     * 获取待审核列表
     */
    @GetMapping("/pending")
    public PageResponseVO<UserAuditVO> getPendingAudits(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Long count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Long page) {
        
        return userAuditService.getPendingAudits(page, count);
    }

    /**
     * 获取所有审核记录
     */
    @GetMapping("/list")
    public PageResponseVO<UserAuditVO> getAuditList(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Long count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Long page,
            @RequestParam(name = "status", required = false) String status,
            @RequestParam(name = "keyword", required = false) String keyword) {
        
        return userAuditService.getAuditList(page, count, status, keyword);
    }

    /**
     * 获取审核详情
     */
    @GetMapping("/{id}")
    public UserAuditVO getAuditDetail(@PathVariable @Positive(message = "{id.positive}") Long id) {
        return userAuditService.getAuditDetail(id);
    }

    /**
     * 审核通过
     */
    @PostMapping("/{id}/approve")
    public UpdatedVO approveAudit(
            @PathVariable @Positive(message = "{id.positive}") Long id,
            @RequestParam(required = false) String remark) {
        
        userAuditService.approveAudit(id, remark);
        return new UpdatedVO();
    }

    /**
     * 审核拒绝
     */
    @PostMapping("/{id}/reject")
    public UpdatedVO rejectAudit(
            @PathVariable @Positive(message = "{id.positive}") Long id,
            @RequestParam String remark) {
        
        userAuditService.rejectAudit(id, remark);
        return new UpdatedVO();
    }

    /**
     * 获取审核统计
     */
    @GetMapping("/stats")
    public Object getAuditStats() {
        return userAuditService.getAuditStats();
    }

    /**
     * 批量审核通过
     */
    @PostMapping("/batch-approve")
    public UpdatedVO batchApproveAudits(@RequestBody List<Long> ids) {
        userAuditService.batchApproveAudits(ids);
        return new UpdatedVO();
    }

    /**
     * 批量审核拒绝
     */
    @PostMapping("/batch-reject")
    public UpdatedVO batchRejectAudits(
            @RequestBody List<Long> ids,
            @RequestParam String remark) {
        
        userAuditService.batchRejectAudits(ids, remark);
        return new UpdatedVO();
    }
} 